﻿using System;
using Microsoft.Practices.Prism.Regions;

namespace MassPokerTool.Infrastructure.Navigation
{
    public static class NavigationExtensions
    {
        #region RegionManager Extensions

        public static void Navigate(this IRegionManager regionManager, string regionName, Uri target, Action<NavigationResult> navigationCallback, INavigationContextParameter navigationParameter)
        {
            if (regionManager == null)
            {
                navigationCallback(new NavigationResult(new NavigationContext(null, target), false));
                return;
            }

            if (regionManager.Regions.ContainsRegionWithName(regionName))
                regionManager.Regions[regionName].Navigate(target, navigationCallback, navigationParameter);
            else
                navigationCallback(new NavigationResult(new NavigationContext(null, target), false));
        }

        public static void Navigate(this IRegionManager regionManager, string regionName, string target, INavigationContextParameter navigationContextParameter = null)
        {
            Navigate(regionManager, regionName, new Uri(target, UriKind.RelativeOrAbsolute), (nr) => { }, navigationContextParameter);
        }

        #endregion

        #region Region Extensions

        public static void Navigate(this IRegion region, Uri target, Action<NavigationResult> navigationCallback, INavigationContextParameter navigationContextParameter)
        {
            if (region == null)
                return;

            region.Context = navigationContextParameter;
            region.RequestNavigate(target, navigationCallback);
        }

        #endregion

        #region NavigationContext Extensions

        public static INavigationContextParameter GetNavigationParameter(this NavigationContext context)
        {
            if (context == null || context.NavigationService == null || context.NavigationService.Region == null)
                return null;

            if (context.NavigationService.Region.Context is INavigationContextParameter)
                return context.NavigationService.Region.Context as INavigationContextParameter;

            return null;
        }

        #endregion
    }

}
